package com.renwei.yunlong.activity.news;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.ReturnBean;
import com.renwei.yunlong.event.ProblemPageRefreshEvent;
import com.renwei.yunlong.global.AppHelper;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.SimpleOptionView;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * 问题关闭--页面
 * <p>
 * Created by Administrator on 2017/8/29.
 */

public class ProblemCloseActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.tv_input_title)
    TextView tvInputTitle;
    @Bind(R.id.et_input_content)
    EditText etInputContent;
    @Bind(R.id.tv_input_number)
    TextView tvInputNumber;
    @Bind(R.id.btn_confirm)
    Button btnConfirm;
    private String questionId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_problem_close);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        questionId = getIntent().getStringExtra("questionId");

        initData();
    }

    private void initData() {
        btnConfirm.setOnClickListener(this);
        simpleTileView.setTitle("关闭");

        tvInputTitle.setText("关闭原因");
        etInputContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100), AppHelper.emojiFilter});
        etInputContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                int length = s.length();
                tvInputNumber.setText(length + "");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                String content = etInputContent.getText().toString();
                if (TextUtils.isEmpty(content)) {
                    showCenterInfoMsg("请输入关闭原因");
                    return;
                }
                HashMap<String, String> map = new HashMap<>();
                if ("1".equals(companyType)) {//业主
                    map.put("currentUserId", ownerBean.getRows().getEmployeeId());
                } else if ("2".equals(companyType)) {//服务商
                    map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
                }
                map.put("questionId", questionId);
                map.put("result", content);
                map.put("flag", "7");

                ServiceRequestManager.getManager().setProblemClose(this, JsonMapUtil.mapToJson(map), this);

                break;
            default:
                break;
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                ReturnBean bean = new Gson().fromJson(result, ReturnBean.class);
                if (bean.getMessage().getCode() == 200) {
                    showCenterSuccessMsg("操作成功");
                    finish();
                } else if (bean.getMessage().getCode() == 1008) {
                    showCenterInfoMsg("无权限访问");
                }
                EventBus.getDefault().post(new ProblemPageRefreshEvent());
                break;
            default:
                break;
        }
    }

    @Override    public void onException(int requestId, String code) {        showTopWrongMsg(getResources().getString(R.string.net_work_error));    }

    public static void OpenActivity(Context context, String questionId) {
        Intent intent = new Intent(context, ProblemCloseActivity.class);
        intent.putExtra("questionId", questionId);
        context.startActivity(intent);
    }
}
